package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.service.CategoryBrandService;
import com.leyou.item.service.CategoryService;

import com.sun.xml.internal.bind.v2.model.core.ID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {


    @Autowired
    private CategoryService categoryService;

    @Autowired
    private CategoryBrandService categoryBrandService;


    //TODO 根据父类目id，查询子类目的集合
    @Override
    public List<CategoryDTO> findCategoryById(Long pid) {


       /* List<Category> categoryById = baseMapper.findCategoryById(pid);
        List<CategoryDTO> categoryDTOS = CategoryDTO.convertEntityList(categoryById);
*/
       // categoryService.list(new QueryWrapper<Category>(pid))
        List<Category> categoryList = query().eq("parent_id", pid).list();

        List<CategoryDTO> categoryDTOS = CategoryDTO.convertEntityList(categoryList);

        return categoryDTOS;
    }



    @Override
    public CategoryDTO findCategoryDTOById(Long id) {
    /*    //为什么返回的是dto呢
        Category category = baseMapper.selectById(id);
        *//*   Category category = baseMapper.selectCategoryById(id);*//*
        System.out.println(category + "@@@@@@@@");
        CategoryDTO categoryDTO = new CategoryDTO(category);
*/

        Category category = categoryService.getById(id);
        CategoryDTO categoryDTO = new CategoryDTO(category);

        return categoryDTO;
    }

    @Override
    public List<CategoryDTO> listCategoryByIds(List<Long> ids) {

/*
        CategoryDTO categoryDTO = new CategoryDTO();
        for (Long id : ids) {
            Category byId = categoryService.getById(id);
             categoryDTO = new CategoryDTO(byId);
        }*/

        List<Category> categories = categoryService.listByIds(ids);
        List<CategoryDTO> categoryDTOS = CategoryDTO.convertEntityList(categories);


        return categoryDTOS;
    }



    //todo 根据品牌id 查询分类的集合
    @Override
    public List<CategoryDTO> listCategoryByBrandId(Long bid) {
         //根据品牌id，查询商品分类的集合

   /*     List<Category> categories = baseMapper.listCategoryByBrandId(bid);

        List<CategoryDTO> categoryDTOS = CategoryDTO.convertEntityList(categories);*/



        List<CategoryBrand> categoryBrandList = categoryBrandService.query().eq("brand_id", bid).list();
        //获取分类表集合
        List<Long> cids = new ArrayList<>();
        for (CategoryBrand categoryBrand : categoryBrandList) {
            Long categoryId = categoryBrand.getCategoryId();
            cids.add(categoryId);

        }
        List<Category> categories = this.listByIds(cids);
        List<CategoryDTO> categoryDTOS = CategoryDTO.convertEntityList(categories);

        return categoryDTOS;
    }


}
